package com.coc.controller.system;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coc.core.utils.CheckObjField;
import com.coc.core.utils.StringRelaUtils;
import com.coc.entity.system.SysPermission;
import com.coc.entity.system.SysRolePermission;
import com.coc.service.system.SysPermissionService;
import com.coc.service.system.SysRolePermissionService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.coc.entity.core.JsonResult;
import com.coc.entity.core.ResultTool;
import java.util.List;

/**
 * (SysPermission)表控制层
 *
 * @author songju
 * @since 2020-02-19 11:34:01
 */
@RestController
@RequestMapping("sysPermission")
public class SysPermissionController extends ApiController {
    /**
     * 服务对象
     */
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param sysPermission 查询实体
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    public JsonResult selectAll(Page<SysPermission> page, SysPermission sysPermission) {
        JsonResult result = ResultTool.success();
        QueryWrapper<SysPermission> sqw = new CheckObjField<SysPermission>().getFields(sysPermission);
        result.setData(this.sysPermissionService.page(page, sqw));
        return result;
    }
    @PostMapping("/selectAll1")
    public JsonResult selectAll1(Page<SysPermission> page, SysPermission sysPermission) {
        JsonResult result = ResultTool.success();
        result.setData(this.sysPermissionService.list());
        return result;
    }
    @PostMapping("/getPermitById")
    public JsonResult getPermitById(Page<SysPermission> page, SysPermission sysPermission) {
        JsonResult result = ResultTool.success();
        List<SysPermission> permitById = sysPermissionService.getPermitById();
        if (permitById!=null){
            result.setData(permitById);
            return result;
        }else {
           return ResultTool.fail();
        }

    }
    //根据id批量删除
      @PostMapping("/delBatchByIds")
    public JsonResult delBatchByIds(@RequestBody String ids){
        JSONObject jsonObject = JSONObject.parseObject(ids);
        String idss = jsonObject.getString("id");
        List<String> ss = StringRelaUtils.strToList(idss);
        boolean b = sysPermissionService.removeByIds(ss);
          QueryWrapper<SysRolePermission> spqw = new QueryWrapper<>();
          spqw.in("permit_id",ss);
          sysRolePermissionService.remove(spqw);
          if (b){
            return ResultTool.success("删除成功");
        }else {
            return ResultTool.fail();
        }
    }

    @PostMapping("/addPermit")
    public JsonResult addPermit(@RequestBody String json) {
        sysPermissionService.addPermit(json);
        return ResultTool.success();
    }
}